package com.item.controller;


import com.github.pagehelper.PageInfo;
import com.item.bean.Result;
import com.item.domain.TbUser;
import com.item.po.TbUserPo;
import com.item.service.TbUserService;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

@RestController
public class TbUserController {

    @Autowired
    TbUserService tbUserService;

    @PostMapping(value = "/tbUser/{limit}/{page}")
    public PageInfo<TbUserPo> TbUser(
            @PathVariable(value = "limit", required = false) int limit,
            @PathVariable(value = "page", required = false) int page,
            @RequestBody(required = false) TbUser tbUser) {
        Result result = new Result();

        List<TbUserPo> tbUsers = tbUserService.findAll(limit, page, tbUser);
        PageInfo<TbUserPo> pageInfo = new PageInfo<>(tbUsers);

        return pageInfo;
    }

    @PostMapping(value = "/tbUser/list/{limit}/{page}")
    public PageInfo<TbUserPo> TbUserList(
            @PathVariable(value = "limit", required = false) int limit,
            @PathVariable(value = "page", required = false) int page,
            @RequestBody(required = false) TbUser tbUser) {
        Result result = new Result();

        List<TbUserPo> tbUsers = tbUserService.findAllList(limit, page, tbUser);
        PageInfo<TbUserPo> pageInfo = new PageInfo<>(tbUsers);

        return pageInfo;
    }

    @PostMapping(value = "/tbUser/to/list/add")
    public Integer TbUserAdd(@RequestBody TbUser tbUser) {

        int i = tbUserService.add(tbUser);
        return i;
    }

    @PutMapping(value = "/tbUser/to/list/update")
    public Integer tbUserUpdate(@RequestBody TbUser tbUser) {

        int i = tbUserService.update(tbUser);
        return i;
    }

    @DeleteMapping(value = "/tbUser/{id}")
    public Integer tbUserDelete(@PathVariable(value = "id") int id) {

        int i = tbUserService.delete(id);
        return i;
    }

    @GetMapping(value = "/tbUser/{id}")
    public TbUser tbUserGet(@PathVariable(value = "id") int id) {

        return tbUserService.getUserId(id);
    }

    @GetMapping(value = "/tbUser/list/{userId}")
    public List<TbUser> tbUserList(@PathVariable(value = "userId") int userId) {
        return tbUserService.getUserList(userId);
    }


    @GetMapping(value = "/tbUser/userInfo/list/{id}")
    public TbUserPo tbUserListFindAll(@PathVariable(value = "id") int id) {
        return tbUserService.tbUserUserInfoList(id);
    }

    @PostMapping(value = "/tbUser/userInfo/idsArray/{ids}")
    public List<TbUser> tbUserUserInfodisArray(
            @PathVariable(value = "ids", required = false) String ids,
            @RequestBody(required = false) Object object) {
        List<String> stringList = new ArrayList<>();
        for (String s : ids.split(",")) {
            stringList.add(s);
        }
        List<TbUser> tbUsers = tbUserService.idsArray(stringList);
        System.out.println(object);
        System.out.println(ids);
        return tbUsers;
    }

    @PostMapping(value = "/tbUser/user/name")
    public List<TbUser> tbUserName(@RequestBody(required = false) TbUser tbUser) {
        return tbUserService.tbUserName(tbUser);
    }

    @GetMapping(value = "/tbUser/count/{id}")
    public TbUserPo tbUserGetCount(@PathVariable(value = "id") int id) {

        return tbUserService.getUserCountId(id);
    }

    @GetMapping(value = "/tbUser/beauty/{id}")
    public TbUser tbUserBeauty(@PathVariable(value = "id") int id) {

        return tbUserService.tbUserBeauty(id);
    }

    @PostMapping(value = "/api/user/add")
    public Integer tbPaiUserAdd(@RequestBody(required = false) Object object) {

        System.out.println(object);

        return 1;
    }


}
